package com.hrvsr.common.redis;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class JedisHolder {
	private static final JedisPool jedisPool = newJedisPool();

	public static JedisPool newJedisPool() {
		GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
		poolConfig.setMaxIdle(5);
		poolConfig.setMaxTotal(20);
		poolConfig.setMinIdle(2);
		poolConfig.setTestOnBorrow(true);
		poolConfig.setMinEvictableIdleTimeMillis(10 * 1000);
		// TODO, this host and port shall be placed eleswhere
		return new JedisPool(poolConfig, "localhost", 6379);
	}

	public static JedisPool jedisPool() {
		return jedisPool;
	}

	public static Jedis getJedisFromPool() {
		return jedisPool.getResource();
	}

	public static Jedis getJedisFromPool(int dbNo) {
		Jedis jedis = jedisPool.getResource();
		jedis.select(dbNo);
		return jedis;
	}
}
